package cn.njyd.corejava.thread.myday02;

import java.util.concurrent.*;

public class ThreadPoolDemo01 {
    public static void main(String[] args) {
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                3,//核心线程数量
                5,//最大线程数量
                500L,//临时线程等待最长时间
                TimeUnit.MILLISECONDS,//第三个参数的单位
                new ArrayBlockingQueue<>(3),//指定阻塞队列大小
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()// 指定拒绝策略
        );

        for (int i=0;i<10;i++) {
            final int k = i;
            pool.submit(()->{
                System.out.println(Thread.currentThread().getName()+"正在准备为第"+(k+1)+"位用户服务");
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+"" +
                        "正在为第"+(k+1)+"位客户服务");
            });
        }
        pool.shutdown();
    }
}
